﻿using Pay.Core;
using Pay.Data;
using Pay.DTO;
using Pay.IService;
using Pay.Service.Cache;
using Pay.TO;
using System;
using System.Linq;

namespace Pay.Service {
    public class InviteHorsePowerSettingService : ServiceBase, IInviteHorsePowerSettingService, IDisposable {
        private IUnitOfWork _iUnitOfWork;
        public InviteHorsePowerSettingService(IUnitOfWork iUnitOfWork) : base(iUnitOfWork) {
            _iUnitOfWork = iUnitOfWork;
        }

        /// <summary>
        /// 获得分页列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public PagedResult<invite_horse_power_setting_dto> GetInviteHorsePowerSettings(InviteHorsePowerSettingQuery query) {
            var filter = PredicateBuilder.True<InviteHorsePowerSetting>();
            if (!string.IsNullOrEmpty(query.status)) {
                int value = int.Parse(query.status);
                filter = filter.And(c => c.Status == (EInviteHorsePowerSettingStatus)value);
            }
            if (!string.IsNullOrEmpty(query.begin_time)) {
                DateTime time = DateTime.Parse(query.begin_time);
                filter = filter.And(c => c.CreateTime >= time);
            }
            if (!string.IsNullOrEmpty(query.end_time)) {
                DateTime time = DateTime.Parse(query.end_time).AddDays(1);
                filter = filter.And(c => c.CreateTime <= time);
            }
            var result = context.InviteHorsePowerSetting.FromCachePaged<InviteHorsePowerSetting, invite_horse_power_setting_dto>(query.page, query.size, filter, orderBy => orderBy.OrderBy(o => o.Status).ThenByDescending(o => o.UpdateTime));
            return result;
        }

        /// <summary>
        /// 获得邀请好友马力设置
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public invite_horse_power_setting_dto GetInviteHorsePowerSetting(long id) {
            return context.InviteHorsePowerSetting.Find(id).ToDtoObject<invite_horse_power_setting_dto>();
        }

        /// <summary>
        /// 新增邀请好友马力设置
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public int AddInviteHorsePowerSetting(invite_horse_power_setting_dto dto) {
            if (dto.horse_power < 0)
                throw new ServerErrorException("增加马力不能小于0");
            if (dto.begin_time > dto.end_time)
                throw new ServerErrorException("开始时间不能大于结束时间");
            if (context.InviteHorsePowerSetting.Exist(o => ((dto.begin_time <= o.BeginTime && dto.end_time >= o.BeginTime) || (dto.begin_time <= o.BeginTime && dto.end_time >= o.EndTime)
             || (dto.begin_time >= o.BeginTime && dto.end_time <= o.EndTime) || (dto.begin_time <= o.EndTime && dto.end_time >= o.EndTime)) && o.Status != EInviteHorsePowerSettingStatus.Deleted))
                throw new ServerErrorException("开始时间-结束时间 已被其他记录包含");
            InviteHorsePowerSetting model = new InviteHorsePowerSetting();
            dto.ToDtoObject(model);
            model.CreateTime = DateTime.Now;
            model.UpdateTime = DateTime.Now;
            context.InviteHorsePowerSetting.Add(model);
            int result = _iUnitOfWork.Commit("InviteHorsePowerSetting");
            return result;
        }

        /// <summary>
        /// 更新邀请好友马力设置
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public int UpdateInviteHorsePowerSetting(invite_horse_power_setting_dto dto) {
            InviteHorsePowerSetting model = context.InviteHorsePowerSetting.FindById(dto.id);
            if (dto.horse_power < 0)
                throw new ServerErrorException("增加马力不能小于0");
            if (dto.begin_time > dto.end_time)
                throw new ServerErrorException("开始时间不能大于结束时间");
            if (context.InviteHorsePowerSetting.Exist(o => o.Id != dto.id && ((dto.begin_time <= o.BeginTime && dto.end_time >= o.BeginTime) || (dto.begin_time <= o.BeginTime && dto.end_time >= o.EndTime)
             || (dto.begin_time >= o.BeginTime && dto.end_time <= o.EndTime) || (dto.begin_time <= o.EndTime && dto.end_time >= o.EndTime)) && o.Status != EInviteHorsePowerSettingStatus.Deleted))
                throw new ServerErrorException("开始时间-结束时间 已被其他记录包含");
            dto.ToDtoObject(model);
            model.UpdateTime = DateTime.Now;
            int result = _iUnitOfWork.Commit("InviteHorsePowerSetting");
            return result;
        }

        /// <summary>
        /// 修改邀请好友马力设置状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public int UpdateInviteHorsePowerSettingStatus(long id, int status) {
            InviteHorsePowerSetting model = context.InviteHorsePowerSetting.FindById(id);
            model.Status = (EInviteHorsePowerSettingStatus)(status);
            model.UpdateTime = DateTime.Now;
            int result = _iUnitOfWork.Commit("InviteHorsePowerSetting");
            return result;
        }

        /// <summary>
        /// 获取邀请好友马力设置
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public invite_horse_power_setting_dto GetInviteHorsePowerSettingByTime(DateTime time) {
            return context.InviteHorsePowerSetting.Where(o => o.Status == EInviteHorsePowerSettingStatus.Normal && o.BeginTime <= time && o.EndTime >= time)
                    .FromCacheFirstOrDefault<InviteHorsePowerSetting, invite_horse_power_setting_dto>("InviteHorsePowerSetting");
        }
    }
}
